<template>
  <view class="login">
    <view class="logo flex align-center justify-center">
      <image src="/static/logo.png" mode="widthFix"></image>
    </view>
    <view class="title">
      网约车考试秘籍
    </view>
    <view class="desc">
      从业资格证考试
    </view>
    <button class="addbtn" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" type="default"
      v-if="platform == 'dy'">抖音手机号一键登录</button>
    <!-- 	<button class="addbtn" type="default" @click="getPhoneNumber" v-if="platform == 'dy'">一键快捷登录</button> -->
    <button class="addbtn" open-type="getPhoneNumber" type="default" @getphonenumber="getnumber"
      v-if="platform == 'wx'">一键快捷登录</button>
    <button class="addbtn" open-type="getPhoneNumber" type="default" @getphonenumber="getksnumber"
      v-if="platform == 'kuaishou'">快手一键登录</button>
    <view style="text-align: center;margin-top: 25rpx;display: flex;align-items: center;justify-content: center;">
      <u-checkbox active-color="#03BE7A" shape="circle" v-model="checked">已阅读并同意<text style="color: #03BE7A;"
          @click="HandGo('user')">《用户协议》</text>及<text style="color: #03BE7A;"
          @click="HandGo('other')">《隐私条款》</text></u-checkbox>
    </view>
  </view>
</template>

<script>
import {
  doLogin,
  dowxLogin,
  kswxLogin
} from "@/api/user.js"
import {
  mapGetters
} from 'vuex'
export default {
  data () {
    return {
      platform: '',
      checked: false,
      pid: 0
    }
  },
  computed: {
    ...mapGetters('app', {
      pagePid: 'pagePid'
    })
  },
  onLoad (options) {
    uni.login({
      success (e) {
        console.log(e)
        // rel(e.code)
      },
      fail (e) {
        console.log(e)
      }
    })
    // 判断平台
    // #ifdef MP-WEIXIN
    this.platform = 'wx'
    // #endif
    // #ifdef MP-TOUTIAO
    this.platform = 'dy'
    // #endif
    // #ifdef MP-KUAISHOU
    this.platform = 'kuaishou'
    // #endif
  },
  methods: {
    HandGo (e) {
      uni.navigateTo({
        url: '/pages/login/agreement?type=' + e
      })
    },
    async getnumber (e) {
      const code = await this.getCode()
      this.wxLogin({
        code,
        iv: e.detail.iv,
        decode: e.detail.encryptedData,
        province: this.$store.state.app.mylocation.province,
        city: this.$store.state.app.mylocation.city,
        pid: this.$store.state.app.pid
      })
    },
    async getksnumber (e) {
      if (!this.checked) {
        uni.showToast({
          title: '请先同意协议',
          icon: 'none'
        })
        return
      }
      const code = await this.getCode()
      this.ksLogin({
        code,
        iv: e.detail.iv,
        encryptedData: e.detail.encryptedData,
        province: this.$store.state.app.mylocation.province,
        city: this.$store.state.app.mylocation.city
      })
    },
    async getPhoneNumber (e) {
      const code = await this.getCode()
      this.handLogin({
        code,
        encryptedData: e.detail.encryptedData,
        iv: e.detail.iv,
        province: this.$store.state.app.mylocation.province,
        city: this.$store.state.app.mylocation.city
      })
    },
    handClick (e) {
      uni.getUserProfile({
        desc: "univerify",
        success: async (info) => {
          // const code = await this.getCode()
          // this.handLogin({
          // 	code,
          // 	avatar: info.userInfo.avatarUrl,
          // 	nickname: info.userInfo.nickName,
          // 	user_id: this.pagePid
          // })
        }
      })
    },
    async handLogin (params) {
      const {
        data
      } = await doLogin(params)
      this.$store.dispatch('app/setToken', data.token).then(res => {
        uni.switchTab({
          url: '/pages/tabbar/home/index'
        })
      })
    },
    async wxLogin (params) {
      const {
        data
      } = await dowxLogin(params)
      this.$store.dispatch('app/setToken', data.token).then(res => {
        uni.switchTab({
          url: '/pages/tabbar/home/index'
        })
      })
    },
    async ksLogin (params) {
      const {
        data
      } = await kswxLogin(params)
      this.$store.dispatch('app/setToken', data.token).then(res => {
        uni.switchTab({
          url: '/pages/tabbar/home/index'
        })
      })
    },
    getCode () {
      return new Promise((rel, rej) => {
        uni.login({
          success (e) {
            console.log(e)
            rel(e.code)
          }
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login {
  min-height: 100vh;
  background-color: #fff;
  padding-top: 120rpx;

  .logo {
    image {
      width: 128rpx;
      height: auto;
    }
  }

  .title {
    margin-top: 24rpx;
    font-size: 36rpx;
    font-family: PingFang SC-Bold, PingFang SC;
    font-weight: bold;
    color: #333333;
    text-align: center;
  }

  .desc {
    text-align: center;
    margin-top: 8rpx;
    font-size: 26rpx;
    font-family: PingFang SC-Regular, PingFang SC;
    font-weight: 400;
    color: #666666;
  }

  .addbtn {
    margin-top: 104rpx;
    width: 650rpx;
    background-color: #03BE7A;
    color: #fff;

    &::after {
      border: none;
    }
  }
}
</style>